

package com.kimo.shop.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kimo.shop.bean.model.SkuLang;
import com.kimo.shop.common.exception.KimoShopBindException;
import com.kimo.shop.common.i18n.I18nMessage;
import com.kimo.shop.common.i18n.LanguageEnum;
import com.kimo.shop.dao.SkuLangMapper;
import com.kimo.shop.service.SkuLangService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import com.kimo.shop.bean.model.Sku;
import com.kimo.shop.dao.SkuMapper;
import com.kimo.shop.service.SkuService;

/**
 *
 * @author kimo-taozhouchuan on 2018/09/29.
 */
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {

    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SkuLangMapper skuLangMapper;
    @Autowired
    private SkuLangService skuLangService;

	@Override
	@Cacheable(cacheNames = "skuList", key = "#prodId +'-'+#dbLang")
	public List<Sku> listByProdId(Long prodId, Integer dbLang) {
		List<Sku> skuList = skuMapper.listByProdId(prodId);
		if(CollectionUtils.isEmpty(skuList)){
			return null;
		}
		// 将当前语言的prodName给sku
		for (Sku sku : skuList) {
			for (SkuLang skuLang : sku.getSkuLangList()) {
				if(Objects.equals(skuLang.getLang(),dbLang)){
					sku.setProdName(skuLang.getProdName());
				}
			}
		}
		return skuList;
	}

	@Override
	@Cacheable(cacheNames = "sku", key = "#skuId +':'+#lang")
	public Sku getSkuBySkuId(Long skuId, Integer lang) {
		return skuMapper.getSkuBySkuId(skuId,lang);
	}

	@Override
	public List<Sku> getSkuListBySkuId(Long skuId) {
		return skuMapper.getSkuListBySkuId(skuId);
	}

	@Override
	@Caching(evict = {
			@CacheEvict(cacheNames = "sku", key = "#skuId +':'+#lang"),
			@CacheEvict(cacheNames = "skuList", key = "#prodId +'-'+#lang"),
			@CacheEvict(cacheNames = "skuLang", key = "#skuId +':'+#lang"),
			@CacheEvict(cacheNames = "skuList", key = "#prodId")
	})
	public void removeSkuCacheBySkuId(Long skuId, Long prodId, Integer lang) {

	}

	/**
	 *
	 * @param skuList
	 * @param isUpdate false:添加  true:修改
	 */
	@Override
	public void checkPartyCode(List<Sku> skuList, boolean isUpdate) {
		if (CollectionUtil.isEmpty(skuList)){
			return;
		}
		for (Sku sku:skuList){
			if (Objects.isNull(sku.getPartyCode())){
				continue;
			}
			int count = 0;
			if(StrUtil.isNotBlank(sku.getPartyCode())) {
				count = skuMapper.selectCount(new LambdaQueryWrapper<Sku>()
						.eq(Sku::getPartyCode, sku.getPartyCode())
						.ne(isUpdate, Sku::getSkuId, sku.getSkuId())
				);
			}
			if (count>0){
				// 商品编码已存在
				throw  new KimoShopBindException(I18nMessage.getMessage("kimo.prod.code")+ sku.getPartyCode()
						+I18nMessage.getMessage("kimo.prod.user.cash.info"));
			}
		}

	}

    @Override
    public void insertBatchAndLang(List<Sku> skuList) {
		// 移除空字符串
		for (Sku sku : skuList) {
			if (StrUtil.isBlank(sku.getPic()) && Objects.nonNull(sku)) {
				sku.setPic(null);
			}
		}
		skuMapper.insertBatchReturnId(skuList);
		// 保存语言表
		saveSkuLang(skuList);
    }

	@Override
	public void updateSku(Sku sku) {
		// 移除空字符串
		if (StrUtil.isBlank(sku.getPic()) && Objects.nonNull(sku)) {
			sku.setPic(null);
		}
		skuMapper.updateById(sku);
		if(StrUtil.isNotBlank(sku.getProdNameEn())) {
			skuLangMapper.updateSkuLangById(sku.getSkuId(), LanguageEnum.LANGUAGE_EN.getLang(), sku.getPropertiesEn(), sku.getSkuNameEn(), sku.getProdNameEn());
		}
		skuLangMapper.updateSkuLangById(sku.getSkuId(),LanguageEnum.LANGUAGE_ZH_CN.getLang(),sku.getProperties(),sku.getSkuName(),sku.getProdNameCn());
	}

    @Override
    public Sku getSkuAndLangBySkuId(Long skuId, Integer dbLang) {
        Sku sku = skuMapper.getSkuAndLangBySkuId(skuId);
        if(Objects.equals(I18nMessage.getDbLang(),1) && StrUtil.isNotBlank(sku.getProdNameEn())){
            sku.setProdName(sku.getProdNameEn());
            sku.setSkuName(sku.getSkuNameEn());
        }
        return sku;
    }

    private void saveSkuLang(List<Sku> skuList) {
		List<SkuLang> skuLangs = new ArrayList<>();
		for (Sku sku : skuList) {
			SkuLang skuLang = new SkuLang();
			skuLang.setProdName(sku.getProdNameCn());
			skuLang.setProperties(sku.getProperties());
			skuLang.setSkuId(sku.getSkuId());
			skuLang.setLang(LanguageEnum.LANGUAGE_ZH_CN.getLang());
			skuLang.setSkuName(sku.getSkuName());
			skuLangs.add(skuLang);
			if(StrUtil.isNotBlank(sku.getProdNameEn())) {
				SkuLang skuLangEn = new SkuLang();
				skuLangEn.setProdName(sku.getProdNameEn());
				skuLangEn.setProperties(sku.getPropertiesEn());
				skuLangEn.setSkuId(sku.getSkuId());
				skuLangEn.setSkuName(sku.getSkuNameEn());
				skuLangEn.setLang(LanguageEnum.LANGUAGE_EN.getLang());
				skuLangs.add(skuLangEn);
			}
		}
		skuLangService.saveBatch(skuLangs);
	}

}
